﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace SevenDragon
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        System.Uri url;
        List<string> uids;
        List<UserBase> ulst;
        int ipage = 1;


        private void frmMain_Load(object sender, EventArgs e)
        {
            this.uids = new List<string>();
            this.ulst = new List<UserBase>();

            //测试浏览器类
            //WebBrowser wb = new WebBrowser();
            //System.Uri url = new Uri("http://d1.7l.mop.com/mindex");
            //wb.Url = url;
            //wb.Navigated += new WebBrowserNavigatedEventHandler(wb_Navigated);

            //浏览器加载成功
            this.url = new Uri("http://d1.7l.mop.com/statistics?orderby=population&pageId=1");
            this.webBrowser1.Url = url;

            //正向地图测试成功
            //MessageBox.Show((new MapConvert().getXY("6740821").X.ToString()) + "   " + (new MapConvert().getXY("6740821").Y.ToString()));

            //反向地图测试成功
            //var point = new XY(273, -78);
            //MessageBox.Show(new MapConvert().getID(point));

        }
        
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            ////DOM成功
            //IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
            //foreach (IHTMLElement vElement in vDocument.all)
            //{
            //    if ((vElement.tagName.ToUpper() == "A") && (vElement.innerHTML == "地图"))
            //    {
            //        vElement.click();
            //    }
            //}

        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            this.url = new Uri(textBox1.Text.Trim());
            this.webBrowser1.Url = url;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (this.webBrowser1.Url.ToString().Substring(0, 58) != "http://d1.7l.mop.com/statistics?orderby=population&pageId=")
            {
                return;
            }
            if (this.webBrowser1.Url.ToString().Substring(58) != ipage.ToString())
            {
                return;
            }
            try
            {
                string link = "http://d1.7l.mop.com/userview?userid=";
                //DOM成功
                IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
                IHTMLElementCollection myLiks = vDocument.links;
                try
                {
                    foreach (HTMLAnchorElementClass el in myLiks)
                    {
                        if (el.href.Length > 37)
                        {
                            if (el.href.Substring(0, 37) == link)
                                ulst.Add(new UserBase(el.href.Substring(37), el.innerText));
                        }
                    }
                }
                catch { }

                printuser();

                if (this.ipage > 682)
                {
                    Application.Exit();
                }

                if (this.webBrowser1.Url.ToString().Substring(0, 58) != "http://d1.7l.mop.com/statistics?orderby=population&pageId=")
                {
                    return;
                }

                ipage++;
                this.url = new Uri("http://d1.7l.mop.com/statistics?orderby=population&pageId=" + ipage);
                this.webBrowser1.Url = url;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.ulst.Clear();
            }
        }


        #region 时间控制

        private const int START_ID = 1;
        private const int END_ID = 40000;//32999;
        private static int ID = 1;

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ID < END_ID && !webBrowser1.IsBusy)
            {
                ID = ID + 1;
                webBrowser1.Navigate("http://xxxx.com/xx.php?id=" + ID.ToString());

                //listBox1.Items.Add(ID.ToString() + webBrowser1.DocumentText);

                //InsertIntoDB(webBrowser1.DocumentText, ID);
                //textBox4.Text = ID.ToString();
            }
        }

        #endregion

        #region 测试输出


        void printuser()
        {
            using (SqlConnection con = new SqlConnection("server=.;uid='sa';pwd='35224550';database=test"))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    foreach (UserBase u in this.ulst)
                    {
                        try
                        {
                            cmd.CommandText = "insert into UserBase values(" + u.Id.ToString() + ",'" + u.Name + "')";
                            cmd.ExecuteNonQuery();
                        }
                        catch
                        {
                            ulst.Clear();
                        }
                    }
                }
            }
            this.ulst.Clear();
        }

        #endregion
    }
}
